<?php
/**
 * @title 318. 最大单词长度乘积
 * @author start2004
 */

// ini_set("memory_limit", "128M");

// class Solution {
//
//     function find() {
//
//     }
// }

class Solution {

    /**
     * @param String[] $words
     * @return Integer
     */
    function maxProduct($words) {
        /**
         * @since 2023-11-07 26个字母
         */
        $letterArray = [
            "a" => 0,
            "b" => 1,
            "c" => 2,
            "d" => 3,
            "e" => 4,
            "f" => 5,
            "g" => 6,
            "h" => 7,
            "i" => 8,
            "j" => 9,
            "k" => 10,
            "l" => 11,
            "m" => 12,
            "n" => 13,
            "o" => 14,
            "p" => 15,
            "q" => 16,
            "r" => 17,
            "s" => 18,
            "t" => 19,
            "u" => 20,
            "v" => 21,
            "w" => 22,
            "x" => 23,
            "y" => 24,
            "z" => 25,
        ];

        /**
         * @since 2023-11-07 二进制表示字符串含有的字母情况，不含字母数量
         */
        $numArray = $lenArray = [];


        /**
         * @since 2023-11-07 字符串转换2进制
         */
        $initBinString = str_repeat("0", 26);
        foreach ($words as $word){
            /**
             * @since 2023-11-07 长度
             */
            $len = strlen($word);

            /**
             * @since 2023-11-07 统计
             */
            $binStr = $initBinString;
            for($i=0; $i<$len; $i++){
                $binStr[$letterArray[$word[$i]]] = 1;
            }

            /**
             * @since 2023-11-07 保存
             */
            $numArray[] = bindec($binStr);
            $lenArray[] = $len;
        }

        /**
         * @since 2023-11-07 两两找最大数
         */
        $maximum = 0;
        for($i=0; $i<count($words); $i++){
            $numI = $numArray[$i];
            for($j=$i+1; $j<count($words); $j++){
                $numJ = $numArray[$j];

                /**
                 * @since 2023-11-07 AND位运算
                 */
                if(($numI & $numJ) == 0){
                    $maximum = max($maximum, $lenArray[$i]*$lenArray[$j]);
                } else {}
            }
        }

        /**
         * @return
         */
        return $maximum;
    }
}


/**
 * @url http://127.0.0.1/leetcode/202311/2023.11.06.php
 */
$datas = [
    [["abcw","baz","foo","bar","xtfn","abcdef"]], // 16
];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
foreach ($datas as $data){
    if(count($data) <= 20){
        var_dump($data);
    } else {
        var_dump(count($data));
    }

    $obj = new Solution();
    $result = $obj->maxProduct(...$data);
    // $result = $obj->($xhprof->tree($data));
    // $result = $obj->($xhprof->listNode($data));
    // $result = $obj->find(...$data);
    var_dump($result);
    // if(count($result)<=20){
    //     var_dump($result);
    // } else {
    //     var_dump(count($result));
    // }
    echo str_repeat(PHP_EOL, 3);
    echo str_repeat("<br>", 3);
}

// foreach ($datas as $data){
//     $obj = new $data[0][0](...$data[1][0]);
//
//     for ($i=1; $i<count($data[0]); $i++){
//         $func = $data[0][$i];
//         $param = $data[1][$i];
//         $result = $obj->$func(...$param);
//
//         echo $data[0][$i] ."(\"". implode(",", $data[1][$i]) ."\") ";
//             if($result === true){
//                 echo "True";
//             } elseif($result === false) {
//                 echo "False";
//             } elseif($result === null) {
//                 echo "Null";
//             } elseif(is_array($result)) {
//                 var_dump($result);
//             } else {
//                 echo $result;
//             }
//         echo PHP_EOL;
//     }
//
//     echo str_repeat(PHP_EOL, 3);
// }
$xhprof->end();
